<?php

require_once __DIR__."/TopSdk.php";

class ItemsApi{

    private $sort_arr = ['', 'total_sales_des', 'price_asc', 'price_des', 'tk_rate_asc', 'tk_rate_des'];

    private $client;

    private $req;

    public function __construct($taobao_api_params)
    {
        $this->client = new \TopClient;
        $this->client->appkey = $taobao_api_params['app_key'];
        $this->client->secretKey = $taobao_api_params['secret'];
        $this->adzone_id = $taobao_api_params['adzone_id'];
    }
  
  /**
     * 淘宝搜索商品
     * @param strings
     * @return mixed
     */
    public function search($q, $is_overseas, $is_tmall, $has_coupon, $sort_type, $page_no, $page_size){

        $req = new \TbkDgMaterialOptionalRequest;

        $req->setPageSize($page_size);
        $req->setPageNo($page_no);
        $req->setIsOverseas($is_overseas);
        $req->setIsTmall($is_tmall);
        $sort = $this->sort_arr[$sort_type]??'';
        if($sort){
            $req->setSort($sort);    
        }
        $req->setQ($q);
        $req->setHasCoupon($has_coupon);
        $req->setAdzoneId($this->adzone_id);
        $resp = $this->client->execute($req);
        return $resp;
    }

    /**
     * 淘宝商品详情
     * @param strings
     * @return mixed
     */
    public function detail($num_iid){
        $req = new \TbkItemInfoGetRequest;
        $req->setNumIids($num_iid);
        $req->setPlatform("1");
        $req->setIp("11.22.33.43");
        $resp = $this->client->execute($req);
        return $resp;
    }

    /**
     * 搜索淘宝商品，应用渠道分成之后的搜索结果
     * @param strings
     * @return mixed
     */
    public function channelSearch($channelId, $q, $is_overseas, $is_tmall, $has_coupon, $sort_type, $page_no, $page_size){
        
    }

    /**
     * 获取商品详情，应用渠道分成之后的搜索结果
     * @param strings
     * @return mixed
     */
    public function channelDetail($channelId, $num_iid){
        
    }
}

?>